package com.kzj.laimeimei.global;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

import com.ab.util.AbSharedUtil;
import com.alibaba.fastjson.JSON;
import com.baidu.mapapi.SDKInitializer;
import com.kzj.laimeimei.beans.UserBean;
import com.kzj.laimeimei.util.ImageUtil;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by xyg on 2015/9/8.
 */
public class App extends Application {
    public int screenWidth;//屏幕宽度
    public int screenHeight;//屏幕高度
    public String latitude;//纬度
    public String longitude;//经度
    public String city;//城市名
    public String locationCity;//定位的城市名
    public String cookie;
    private UserBean userBean;
    private static App instance;
    public static ImageLoader imageLoader = ImageLoader.getInstance();

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Constant.SystemContext.ROOT_URL_VALUE = Constant.SystemContext.OfficialUrl;
        //  Fresco.initialize(this);
       // LeakCanary.install(this);

        //设置崩溃处理
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        instance = this;
        SDKInitializer.initialize(getApplicationContext());
        WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        screenWidth = windowManager.getDefaultDisplay().getWidth();
        screenHeight = windowManager.getDefaultDisplay().getHeight();
        initImageLoader(getApplicationContext());
        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
    }

    public static App getInstance() {
        return instance;
    }

    /**
     * 描述：Toast提示文本.
     *
     * @param text 文本
     */
    public static void showToast(String text) {
        if (!TextUtils.isEmpty(text)) {
            android.widget.Toast.makeText(App.getInstance(), text, android.widget.Toast.LENGTH_SHORT).show();
        }

    }

    public String getCookie() {
        return cookie;
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
    }

    /**
     * 描述：Toast提示文本.
     *
     * @param resId 文本的资源ID
     */
    public static void showToast(int resId) {
        android.widget.Toast.makeText(App.getInstance(), "" + App.getInstance().getResources().getText(resId), android.widget.Toast.LENGTH_SHORT).show();
    }

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                        // .memoryCache(new UsingFreqLimitedMemoryCache(4*1024*1024))
                .memoryCacheExtraOptions(480, 800)
                        // max width, max height
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .denyCacheImageMultipleSizesInMemory()// 自动缩放
//				.writeDebugLogs() // Remove for release app
                .defaultDisplayImageOptions(ImageUtil.OptionsNormal())
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

    /**
     * 这个方法是用来判断是否是网络图片来的
     * 不是网络图片就加file
     *
     * @param uri
     * @param imageView
     * @param options
     */
    public static void displayImageHttpOrFile(String uri, ImageView imageView, DisplayImageOptions options) {
        if (uri != null) {


            if (uri.startsWith("http://")) {
                imageLoader.displayImage(uri, imageView, options);
            } else if (uri.startsWith("file://")) {
                imageLoader.displayImage(uri, imageView, options);
            } else {
                imageLoader.displayImage("file://" + uri, imageView, options);
            }
        } else {
            imageLoader.displayImage("", imageView, options);
        }
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getLocationCity() {
        return locationCity;
    }

    public void setLocationCity(String locationCity) {
        this.locationCity = locationCity;
    }

    public UserBean getUserBean() {

        if (userBean == null) {
            String userBeanStr = AbSharedUtil.getString(getApplicationContext(), Constant.SystemContext.UserBean);
            if (userBeanStr != null)
                userBean = JSON.parseObject(userBeanStr, UserBean.class);
        }
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        if (userBean != null)
            AbSharedUtil.putString(App.getInstance().getApplicationContext(), Constant.SystemContext.UserBean, JSON.toJSONString(userBean));
        this.userBean = userBean;
    }
}
